package com.gdcp.service.impl;

import com.gdcp.beans.Banner;
import com.gdcp.beans.Product;
import com.gdcp.common.ResponseModel;
import com.gdcp.config.RequestHolder;
import com.gdcp.config.annotation.RedisCache;
import com.gdcp.mapper.BannerMapper;
import com.gdcp.service.BannerService;
import com.gdcp.util.ImageDto;
import com.gdcp.util.ImageUtil;
import com.gdcp.util.IpUtil;
import com.gdcp.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨华京
 * @version 1.0
 * @description
 * @since 1.8
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    ResponseModel responseModel;
    @Autowired
    BannerMapper bannerMapper;

    @Override
    public Map<String,Object> findPageBreakByCondition(Map<String, Object> model) {
        //bootstrap-table要求服务器返回的json须包含：totlal，rows
        Map<String,Object> result = new HashMap<>();
        int total=bannerMapper.findPageBreakByCondition(null).size();
        List<Banner> rows=bannerMapper.findPageBreakByCondition(model);
        if (rows.size()==0){
            int count= (int) model.get("a");
            count=count-(int) model.get("b");
            if (count>0){
                model.put("a",count);
                rows=bannerMapper.findPageBreakByCondition(model);
            }
        }
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public Map<String,Object> removeByPrimaryKey(Integer[] ids) {
        try{
            for (Integer id:ids) {
                bannerMapper.removeByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return responseModel.setModel(false,"删除["+ids.length+"条记录失败"+e.toString());
        }
        return responseModel.setModel(true,"成功删除["+ids.length+"条记录");
    }

    @Override
    public Map<String,Object> insert(Banner banner, ImageDto mainImage) {
        return insertImage("insert",banner, mainImage);
    }

    @Override
    public Map<String,Object> updateInformation(Banner banner, ImageDto mainImage) {
        return insertImage("edit",banner, mainImage);
    }

    private Map<String,Object> insertImage(String str,Banner banner, ImageDto mainImage) {
        boolean str_boolean=str.equals("edit");

        if (mainImage!=null) {
            String dest;
            if (!str_boolean){
                dest= PathUtil.getShopImagePath(101);
            }else{
                dest = PathUtil.getShopImagePath(banner.getId());
            }

            try {
                String imageAddr = ImageUtil.generateThumbnail(mainImage, dest);
                banner.setImage(imageAddr);
            } catch (UnsupportedEncodingException e) {
                return responseModel.setModel(false,e.toString());
            }
        }

        if (str_boolean) {
            try {
                bannerMapper.updateByPrimaryKey(banner);
                return responseModel.setModel(true,"修改成功");
            }catch (Exception e){
                e.printStackTrace();
                return responseModel.setModel(false,"修改失败");
            }
        }else {
            try {
                bannerMapper.insert(banner);
                return responseModel.setModel(true,"新增成功");
            }catch (Exception e){
                return responseModel.setModel(false,"新增失败");
            }
        }
    }

    @Override
    public Banner getInformation(Integer id) {
        return bannerMapper.getInformation(id);
    }

    @Override
    public Map<String, Object> searchInformation(Map<String, Object> model) {
        Map<String,Object> result = new HashMap<>();
        int total=bannerMapper.searchInformation(null).size();
        List<Banner> rows=bannerMapper.searchInformation(model);
        result.put("total",total);
        result.put("rows",rows);
        return result;
    }

    @Override
    @RedisCache(flush = true)
    public List<Banner> queryByStatus() {
        List<Banner> banners=bannerMapper.queryByStatus();
        return banners;
    }
}
